package com.doraz.orb.utils;

import android.util.Log;

public class IntValidator {
	private int defaultVal = 0;
	private int min = 0;
	private int max = 0;
	private boolean hasMin = false;
	private boolean hasMax = false;
	
	private final static String TAG = "IntValidator";
	
	/**
	 * Default Constructor
	 */
	public IntValidator() {
		
	}
	
	/**
	 * 
	 * @param defaultVal	the default value to return (on failed parses)
	 */
	public IntValidator(int defaultVal) {
		this.defaultVal = defaultVal;
	}
	
	/**
	 * 
	 * @param defaultVal	the default value to return (on failed parses)
	 * @param min			the minimum value
	 */
	public IntValidator(int defaultVal, int min) {
		this.defaultVal = defaultVal;
		this.min = min;
		this.hasMin = true;
	}
	
	/**
	 * Parses an int from a String
	 * 
	 * @param intStr	the String to parse
	 * @return			the int, or defaultValue if failed
	 */
	public int parseInt(String intStr)
	{
		int val = defaultVal;
		
		if(intStr != null)
		{
			try
			{
				val = new Integer(intStr).intValue();
				if(hasMin && val < min) {
					return defaultVal;
				}
				
				if (hasMax && val > max) {
					return defaultVal;
				}
			}
			catch(NumberFormatException nfe)
			{
				Log.w(TAG, "Invalid Number: "+intStr);
			}
		}
		
		return val;		
	}

	/**
	 * @return the defaultVal
	 */
	public int getDefaultVal() {
		return defaultVal;
	}

	/**
	 * @param defaultVal the defaultVal to set
	 */
	public void setDefaultVal(int defaultVal) {
		this.defaultVal = defaultVal;
	}

	/**
	 * @return the min
	 */
	public int getMin() {
		return min;
	}

	/**
	 * @param min the min to set
	 */
	public void setMin(int min) {
		this.min = min;
		this.hasMin = true;
	}

	/**
	 * @return the max
	 */
	public int getMax() {
		return max;
	}

	/**
	 * @param max the max to set
	 */
	public void setMax(int max) {
		this.max = max;
		this.hasMax = true;
	}
	
	
}
